package com.liuxy.javautil;

import java.util.HashMap;
import java.util.Map;

public class HttpCache {
	private static Map<String, Vaule> httpCache=new HashMap<String, HttpCache.Vaule>();

	public static String doPost(String path,Map<String, String> mapParams) {
		StringBuffer buffer=new StringBuffer("POST");
		buffer.append(path);
		if (mapParams!=null) {
			for (String key : mapParams.keySet()) {
				buffer.append(key).append("=").append(mapParams.get(key)+'&');
			}
		}
		String key=buffer.toString();
		Vaule back=httpCache.get(key);
		if (back!=null) {
			if (System.currentTimeMillis()-back.time<1000*60) {
				return back.back;
			}
			httpCache.remove(key);
		}else {
			back=new Vaule();
		}
		back.back=HttpURLConnectionUtil.doPost(path, mapParams);
		back.time=System.currentTimeMillis();
		httpCache.put(key, back);
		return back.back;
	}
	
	public static String doGet(String path) {
		StringBuffer buffer=new StringBuffer("GET");
		buffer.append(path);
		String key=buffer.toString();
		Vaule back=httpCache.get(key);
		if (back!=null) {
			if (System.currentTimeMillis()-back.time<1000*1) {
				return back.back;
			}
			httpCache.remove(key);
		}else {
			back=new Vaule();
		}
		back.back=HttpURLConnectionUtil.doGet(path);
		back.time=System.currentTimeMillis();
		httpCache.put(key, back);
		return back.back;
	}
	private static class Vaule{
		private String back;
		private long time;
	}
}
